package com.wen.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.common.R;
import com.wen.mapper.SupplierMapper;
import com.wen.pojo.PurchaseOrder;
import com.wen.pojo.Supplier;
import com.wen.pojo.dto.SupplierDto;
import com.wen.pojo.dto.wen.SupplierANDProductDto;
import com.wen.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    @Autowired
    private SupplierMapper mapper;

    @Override
    public Page<Supplier> queryByPage(SupplierDto dto,Long pageNum, Long pageSize) {

         Page<Supplier> page = new Page<>(pageNum, pageSize);

        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getCompanyName())) {
            wrapper.like("company_name", dto.getCompanyName());
        }
        if (!StringUtils.isEmpty(dto.getName())){
            wrapper.like("name",dto.getName());
        }

        return mapper.selectPage(page,wrapper);
    }


    @Override
    public R getSupplierYearMonthsSumTotal(Integer id) {
        List<PurchaseOrder> price = mapper.getSupplierYearMonthsSumTotal(id);
        if (price!=null) {
            return R.ok().data("SupplierSales", price).flag(true);
        }
        return R.error().flag(false);
    }


    @Override
    public List<SupplierANDProductDto> sel(String name,String id) {

        List<SupplierANDProductDto> sel = mapper.sel(name,id);

        return sel;
    }

    @Override
    public int addSupplier(Supplier supplier) {
        supplier.setBalance(0);
        supplier.setCreateDate(new Date());
        supplier.setStatus("C001-1");
        int insert = mapper.insert(supplier);
        return insert;
    }
}
